package com.wbdgj.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wbdgj.R;
import com.wbdgj.ui.home.RechargeResultFailActivity;
import com.wbdgj.ui.home.RechargeResultSuccessActivity;
import com.wbdgj.utils.ToastUtils;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, "wxf1069c4f961f956e");
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d("zp", "onPayFinish, errCode = " + resp.errCode);
		if (resp.errCode == 0){
			ToastUtils.toastShort("支付成功");
			startActivity(new Intent(this, RechargeResultSuccessActivity.class));
			finish();
		}else if(resp.errCode == -1){
			ToastUtils.toastShort("支付失败");
			startActivity(new Intent(this, RechargeResultFailActivity.class).putExtra("result", "支付失败"));
			finish();
		}else if(resp.errCode == -2){
			ToastUtils.toastShort("支付已取消");
			startActivity(new Intent(this, RechargeResultFailActivity.class).putExtra("result", "支付已取消"));
			finish();
		}
//		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle("提示");
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();
//		}
	}
}